home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 January: Mac OS SDK / Dev.CD Jan 99 SDK1.toast / Development Kits / Installer SDK 1.2 / Upgrader 1.2.1 & Engines / Upgrader 1.2.1 / Plug-in Examples / Common Files / Editor Utilities / CTextEditorWindow.cp < prev    next >
Encoding:
Text File  |  1998-08-25  |  3.5 KB  |  158 lines  |  [TEXT/CWIE]

  1.  
  2.  
  3. #include "EditorUtilities.h"
  4.  
  5. #include "CTextEditorWindow.h"
  6. #include "LStyledTextEdit.h"
  7.  
  8. const ResType     kTheTextET                         = 'Text';
  9. const ResType     kRemoveTextB                     = 'rmvB';
  10.  
  11.  
  12.  
  13. SInt32 DoEditText( SInt16 inFileRefNum, SInt16 &inTextRsrcID  )
  14. {
  15.     // Return values:  0 = Changes accepted, 1 = Canceled, no changes made, 2 = Removed,  < 0 = Internal error, no changes mad
  16.     SInt32    result = -1;
  17.     
  18.     SInt16    savedResFile = CurResFile();
  19.     UseResFile( inFileRefNum );
  20.  
  21.     try
  22.     {
  23.         CTextEditorWindow*    theEditorWindow = new CTextEditorWindow( inFileRefNum, inTextRsrcID  );
  24.  
  25.         if( inTextRsrcID == 0 )
  26.             theEditorWindow->mTextRsrcID = GetUniqueIDForResType( inFileRefNum, 'TEXT' );
  27.  
  28.         result = theEditorWindow->DoEdit();
  29.         
  30.         delete theEditorWindow;
  31.         
  32.         if( result == 0 || result == 2  )
  33.             inTextRsrcID = theEditorWindow->mTextRsrcID;
  34.             
  35.     }
  36.     catch( OSErr theErr )
  37.     {
  38.         ;
  39.     }
  40.     
  41.     UseResFile( savedResFile );
  42.     
  43.     return result;
  44. }
  45.  
  46.  
  47. // ---------------------------------------------------------------------------
  48. //        • CTextEditorWindow
  49. // ---------------------------------------------------------------------------
  50. CTextEditorWindow::CTextEditorWindow( SInt16 inFileRefNum, SInt16 inTextRsrcID )
  51.     : StDialogHandler(145, NULL), mFileRefNum(inFileRefNum), mTextRsrcID(inTextRsrcID)
  52. {
  53. }
  54.  
  55. // ---------------------------------------------------------------------------
  56. //        • ~CTextEditorWindow
  57. // ---------------------------------------------------------------------------
  58. CTextEditorWindow::~CTextEditorWindow()
  59. {
  60. }
  61.  
  62.  
  63. SInt32    CTextEditorWindow::DoEdit()
  64. {
  65.     
  66.     LWindow            *theDialog = GetDialog();
  67.  
  68.     this->LoadWindowFromFile();
  69.     
  70.     theDialog->Show();
  71.     
  72.     LStyledTextEdit*    theField = (LStyledTextEdit*) mDialog->FindPaneByID(kTheTextET);
  73.     SignalIf_(theField == nil);
  74.     theDialog->SetLatentSub(theField);
  75.     theField->SelectNone();
  76.  
  77.  
  78.     SInt32    theResult    = -1;
  79.     
  80.     while (true) {
  81.         MessageT    hitMessage = DoDialog();
  82.         
  83.         if (hitMessage == msg_Cancel) {
  84.             theResult = 1;
  85.             break;
  86.             
  87.         } else if (hitMessage == kRemoveTextB) {
  88.         
  89.             Handle theRsrcHandle = Get1Resource( 'TEXT', mTextRsrcID );
  90.             if( theRsrcHandle ) {
  91.                 RemoveResource( theRsrcHandle );
  92.                 DisposeHandle( theRsrcHandle );
  93.             }
  94.             theRsrcHandle = Get1Resource( 'styl', mTextRsrcID );
  95.             if( theRsrcHandle ) {
  96.                 RemoveResource( theRsrcHandle );
  97.                 DisposeHandle( theRsrcHandle );
  98.             }
  99.             mTextRsrcID = 0;
  100.             theResult = 2;
  101.             break;
  102.             
  103.         
  104.         } else if (hitMessage == msg_OK) {
  105.             this->SaveWindowToFile();
  106.             theResult = 0;
  107.             break;
  108.         }
  109.     }
  110.     
  111.     return theResult;
  112. }
  113.  
  114.  
  115. void CTextEditorWindow::SaveWindowToFile()
  116. {
  117.  
  118.     Handle    theTextRsrc = Get1Resource('TEXT', mTextRsrcID );
  119.     RemoveResource( theTextRsrc );
  120.     DisposeHandle( theTextRsrc );
  121.     
  122.     Handle    theStyleRsrc = Get1Resource('styl', mTextRsrcID );
  123.     RemoveResource( theStyleRsrc );
  124.     DisposeHandle( theStyleRsrc );
  125.  
  126.     LStyledTextEdit*    theField = (LStyledTextEdit*) mDialog->FindPaneByID(kTheTextET);
  127.     SignalIf_(theField == nil);
  128.         
  129.     theTextRsrc = theField->GetTextHandle();
  130.     HandToHand( &theTextRsrc );
  131.     AddResource( theTextRsrc, 'TEXT', mTextRsrcID, "\p" ); 
  132.     
  133.     theStyleRsrc = (Handle)theField->GetStyleScrapHandle();
  134.     HandToHand( &theStyleRsrc );
  135.     AddResource( theStyleRsrc, 'styl', mTextRsrcID, "\p" );
  136.     
  137.     UpdateResFile( mFileRefNum );
  138. }
  139.  
  140.  
  141. void CTextEditorWindow::LoadWindowFromFile()
  142. {
  143.  
  144.     Handle    theTextRsrc = Get1Resource('TEXT', mTextRsrcID );
  145.     Handle    theStyleRsrc = Get1Resource('styl', mTextRsrcID );
  146.  
  147.     // Set File Path name
  148.     LStyledTextEdit*    theField = (LStyledTextEdit*) mDialog->FindPaneByID(kTheTextET);
  149.     SignalIf_(theField == nil);
  150.  
  151.     if( theTextRsrc )
  152.         theField->SetTextHandle( theTextRsrc, (TEStyleHandle)theStyleRsrc );
  153.  
  154. }
  155.  
  156.  
  157.  
  158.